#coding:utf-8
import serial,time,checkSum
import os
import logging

from checkSum import hex_checksum
from serialCom import serialCom

FILE = os.getcwd()          #返回当前目录的路径
#logging基础配置，文件名为 os.path.join里面的两个值想加个，输出等级为≥DEBUG级别
logging.basicConfig(filename = os.path.join(FILE,'log.txt'),level = logging.DEBUG,
                    format='%(asctime)s %(filename)s [line:%(lineno)d] %(levelname)s %(message)s')


def setID(meterID):
    print '----------------------------------------------------------'
    print 'case1 设置并读取表号' + '\n' + '                                    '
    logging.debug('*****case 1 设置并读取表号*****')


    #设置并读取表号, 格式
    IDsetFirst = 'FE FE FE FE FE 68 AB 57 58 AA AA AA AA AA F2 77 88 99 55 15 00 0A A0 18 03 00 00'
    # meterID
    # 校验和
    # 16
    # 无接收数据

    for i in range(len(meterID)):

        setData = IDsetFirst.split() + meterID[i].split()
        CheckSum = hex_checksum(setData[5:])
        send_meterId = ''.join(setData + CheckSum + ['16'])
        serialCom(send_meterId)  # 发送设置表号命令

        print '    设置的表号是' + ''.join(meterID[i].split())+' |',
        meterIDread = (
            ''.join(('FE FE FE FE FE 68 AB 57 58 AA AA AA AA AA F2 77 88 99 55 03 00 03 81 0A 01 85 16').split()))
        receive_meterId = serialCom(meterIDread, 35)  # 发送读取表号命令，并返回读取到的表号
        print ' 读取的表号是' + receive_meterId[28:38]+' |',
        if receive_meterId[28:38].upper() == ''.join(meterID[i].split()):
            print '表号 %d 设置并读取正确' % i
            logging.debug(' 设置并读取表号 '+meterID[i]+' 正确')

        else:
            print '表号 %d 设置并读取错误' % i
            logging.debug(' 设置并读取表号' + meterID[i] + '错误')



